package com.example.bailin.giftsay.sort.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SearchGLEntity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by bailin on 16/8/27.
 *
 * 搜索攻略的适配器
 * 不用优化
 */
public class SearchGLAdapter extends BaseAdapter {
    private SearchGLEntity searchGLEntity;
    private int index;

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    public void setSearchGLEntity(SearchGLEntity searchGLEntity) {
        this.searchGLEntity = searchGLEntity;
    }

    @Override
    public int getCount() {
        return searchGLEntity.getData().getPosts() == null ? 0 : searchGLEntity.getData().getPosts().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MyApp.getContext());
        MyViewHolder myViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview_selection, parent, false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        List<SearchGLEntity.DataBean.PostsBean> lists = new ArrayList<>();
        for (int i = 0; i < searchGLEntity.getData().getPosts().size(); i++) {
            lists.add(searchGLEntity.getData().getPosts().get(i));
        }

        NetTool tool = new NetTool();
        switch (index) {
            case 0:
                tool.getImg(searchGLEntity.getData().getPosts().get(position).getCover_image_url(), myViewHolder.imageuUrlSelection);
                tool.getRoundImg(searchGLEntity.getData().getPosts().get(position).getAuthor().getAvatar_url(), myViewHolder.ivAvatarUrl);
                myViewHolder.tvTitle.setText(searchGLEntity.getData().getPosts().get(position).getTitle());
                myViewHolder.tvCategory.setText(searchGLEntity.getData().getPosts().get(position).getColumn().getCategory());
                myViewHolder.tvColumnTitle.setText(searchGLEntity.getData().getPosts().get(position).getColumn().getTitle());
                myViewHolder.tvNicKName.setText(searchGLEntity.getData().getPosts().get(position).getAuthor().getNickname());
                myViewHolder.tvLikesCount.setText("" + searchGLEntity.getData().getPosts().get(position).getLikes_count());
                break;
            case 1:
                Collections.sort(lists, new LikesCount());
                tool.getImg(lists.get(position).getCover_image_url(), myViewHolder.imageuUrlSelection);
                tool.getRoundImg(lists.get(position).getAuthor().getAvatar_url(), myViewHolder.ivAvatarUrl);
                myViewHolder.tvTitle.setText(lists.get(position).getTitle());
                myViewHolder.tvCategory.setText(lists.get(position).getColumn().getCategory());
                myViewHolder.tvColumnTitle.setText(lists.get(position).getColumn().getTitle());
                myViewHolder.tvNicKName.setText(lists.get(position).getAuthor().getNickname());
                myViewHolder.tvLikesCount.setText("" + lists.get(position).getLikes_count());
                break;
        }

        return convertView;
    }

    private class MyViewHolder {
        private ImageView imageuUrlSelection;
        private ImageView ivAvatarUrl;
        private TextView tvTitle;
        private TextView tvCategory;
        private TextView tvColumnTitle;
        private TextView tvNicKName;
        private TextView tvLikesCount;
        public MyViewHolder(View v) {
            imageuUrlSelection = (ImageView) v.findViewById(R.id.cover_image_url_item_selection);
            tvTitle = (TextView) v.findViewById(R.id.title_item_selection);
            tvCategory = (TextView) v.findViewById(R.id.tv_category);
            tvColumnTitle = (TextView) v.findViewById(R.id.tv_column_title);
            tvNicKName = (TextView) v.findViewById(R.id.tv_nickname);
            ivAvatarUrl = (ImageView) v.findViewById(R.id.iv_avatar_url);
            tvLikesCount = (TextView) v.findViewById(R.id.tv_item_selection_number);
        }
    }

    public class LikesCount implements Comparator{
        @Override
        public int compare(Object lhs, Object rhs) {
            SearchGLEntity.DataBean.PostsBean s1 = (SearchGLEntity.DataBean.PostsBean) lhs;
            SearchGLEntity.DataBean.PostsBean s2 = (SearchGLEntity.DataBean.PostsBean) rhs;
            return new Integer(s2.getLikes_count()).compareTo(s1.getLikes_count());
        }
    }
}